package com.dx.mes.web.common.controller;

import com.dx.common.table.ppt.Bis_uas_fnc_grp;
import com.dx.common.table.ppt.Bis_uas_grp;
import com.dx.mes.common.core.his.HistoryManager;
import com.dx.mes.common.transcoder.util.GUIDGenerator;
import com.dx.mes.web.common.service.ISendMessageService;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import static java.util.stream.Collectors.toList;


@Controller
public class SendMessageController {
	/*@Resource
	private HistoryManager historyManager;*/
	@Resource
	private ISendMessageService sendMessageServiceImpl;


	@RequestMapping("sendMsg.do")
	@ResponseBody
	public String sendMessage(String trxId, String strInMsg,HttpServletRequest request) {
		String evt_no = GUIDGenerator.javaGUID();
		String message = sendMessageServiceImpl.sendMesage(trxId, evt_no, strInMsg);
		/*if (historyManager.isTransactionSuccessed(message)) {
			historyManager.sendHistoryMessageToRabbitMQ(evt_no);
		}*/
		return message;
	}
	

	@RequestMapping("/home.do")
	public String rtHome(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		if (request.getSession().getAttribute("user") == null) {
			// 获取request中所有的Cookie，并放在数组中
			Cookie[] cs = request.getCookies();
			// 若Cookie不为空，则遍历所有的Cookie中所有的记录
			if (cs != null && cs.length > 0) {
				for (Cookie c : cs) {
					String cName = c.getName();
					// 查找当前用户的Cookie（此处为sso标识）
					if (cName.equals("ssoName")) {
						// 获取sso中对应的值，即：用户名userName
						String userName1 = c.getValue();
						String userName = URLDecoder.decode(userName1, "utf-8");
						// 把用户名userName放在session中
						request.getSession().setAttribute("user", userName);
					}if (cName.equals("sso")) {
						// 获取sso中对应的值，即：用户名userName
						String userid1 = c.getValue();
						String userid = URLDecoder.decode(userid1, "utf-8");
						// 把用户名userName放在session中
						request.getSession().setAttribute("userid", userid);
					}
				}
			}
		}
		return "redirect:http://localhost:8070/mes-dx-web-login/home.jsp";
	}

}
